package com.example.springtest.grade.cotroller;


import com.example.springtest.common.R;
import com.example.springtest.grade.pojo.Grade;
import com.example.springtest.grade.service.GradeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 成绩控制器
 */
@RestController
@Slf4j
@RequestMapping("grade")
public class GradeController{
    @Autowired
    private GradeService gradeService;

    /**
     * 显示所有成绩
     * @return
     */
    @GetMapping("list")
    public R<List<Grade>> getGradeList(){
        List<Grade> gradeList=gradeService.getGradeList();
        return R.ok(gradeList);
    }

    /**
     * 增加成绩
     * @param grade
     * @return
     */
    @PostMapping("add")
    public R<Void> addGrade(@RequestBody @Validated Grade grade){
        log.debug("新增的成绩：{}",grade);
        gradeService.addGrade(grade);
        return R.ok();
    }

    /**
     * 删除成绩
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public R<Void> deleteGrade(@PathVariable Integer id){
        gradeService.deleteGrade(id);
        return R.ok();
    }

    /**
     * 编辑成绩
     * @param
     * @return
     */
    @PutMapping("edit/{id}")
    public R<Void> editGrade(@PathVariable Integer id, @RequestBody @Validated Grade grade){
        gradeService.editGarde(id,grade);
        return R.ok();
    }
}
